<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>信息管理系统</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <link href="/style/basic_layout.css" rel="stylesheet" type="text/css">
    <link href="/style/common_style.css" rel="stylesheet" type="text/css">
    <script type="text/javascript" src="/js/jquery/jquery.js"></script>
    <script src="/js/plugins/artDialog/jquery.artDialog.js?skin=blue"></script>
    <script type="text/javascript" src="/js/commonAll.js"></script>
    <script src="/js/plugins/jquery-form/jquery.form.min.js"></script>
    <script>
        $(function () {
            // 将左边的全部移动到右边
            moveAll("selectAll", "all_permissions", "self_permissions");
            // 将右边的全部移动到左边
            moveAll("deselectAll", "self_permissions", "all_permissions");

            move("select", "all_permissions", "self_permissions");
            move("deselect", "self_permissions", "all_permissions");

            // 选中所有要提交的option
            selectedOption("self_permissions");

            // 将左边的全部移动到右边
            moveAll("mselectAll", "all_menus", "self_menus");
            // 将右边的全部移动到左边
            moveAll("mdeselectAll", "self_menus", "all_menus");

            move("mselect", "all_menus", "self_menus");
            move("mdeselect", "self_menus", "all_menus");

            // 选中所有要提交的option
            selectedOption("self_menus");

            // 将表单变成 ajax 提交
            $("#editForm").ajaxForm(function (data) {
                // 1. 弹出框，提示操作成功
                var dialog = $.dialog({
                    title: "提示",
                    content: data.msg
                });
                // 如果保存成功，跳转回list
                if (data.success) {
                    dialog.button({name: "确定", callback: function () {
                        window.location.href = "/role/list.do";
                    }});
                } else {
                    // 失败只需要提示一下
                    dialog.button({name: "确定"});
                }
            });

            // 去除所有的权限列表中已选择的权限
            // 1. 获取右边所有option的id
            var ids = $("#self_permissions option").map(function (index, item) {
                return $(item).val();
            });
            // 2. 遍历左边所有的option，判断当前的option的id是否在数组中存在
            $("#all_permissions option").each(function (index, item) {
                if ($.inArray($(item).val(), ids) > -1) {
                    // 3. 如果存在，删除当前的option元素
                    $(item).remove();
                }
            });

            // 去除所有的菜单列表中已选择的菜单
            // 1. 获取右边所有option的id
            var ids = $("#self_menus option").map(function (index, item) {
                return $(item).val();
            });
            // 2. 遍历左边所有的option，判断当前的option的id是否在数组中存在
            $("#all_menus option").each(function (index, item) {
                if ($.inArray($(item).val(), ids) > -1) {
                    // 3. 如果存在，删除当前的option元素
                    $(item).remove();
                }
            });
        });
    </script>
</head>
<body>
<form name="editForm" action="/role/saveOrUpdate.do" method="post" id="editForm">
    <input type="hidden" name="id" value="${role.id}">
    <div id="container">
        <div id="nav_links">
            <span style="color: #1A5CC6;">角色编辑</span>
            <div id="page_close">
                <a>
                    <img src="/images/common/page_close.png" width="20" height="20" style="vertical-align: text-top;"/>
                </a>
            </div>
        </div>
        <div class="ui_content">
            <table cellspacing="0" cellpadding="0" width="100%" align="left" border="0">
                <tr>
                    <td class="ui_text_rt" width="140">角色编号</td>
                    <td class="ui_text_lt">
                        <input name="sn" value="${role.sn}" class="ui_input_txt02"/>
                    </td>
                </tr>
                <tr>
                <tr>
                <tr>
                    <td class="ui_text_rt" width="140">角色名称</td>
                    <td class="ui_text_lt">
                        <input name="name" value="${role.name}" class="ui_input_txt02"/>
                    </td>
                </tr>
                <tr>
                    <td class="ui_text_rt" width="140">权限</td>
                    <td class="ui_text_lt">
                        <table>
                            <tr>
                                <td>
                                    <select multiple="true" class="ui_multiselect01" id="all_permissions">
                                        <c:forEach items="${permissions}" var="item">
                                            <option value="${item.id}">${item.name}</option>
                                        </c:forEach>
                                    </select>
                                </td>
                                <td align="center">
                                    <input type="button" id="select" value="-->" class="left2right"/><br/>
                                    <input type="button" id="selectAll" value="==>" class="left2right"/><br/>
                                    <input type="button" id="deselect" value="<--" class="left2right"/><br/>
                                    <input type="button" id="deselectAll" value="<==" class="left2right"/>
                                </td>
                                <td>
                                    <select multiple="true" class="ui_multiselect01" name="ids" id="self_permissions">
                                        <c:forEach items="${role.permissions}" var="item">
                                            <option value="${item.id}">${item.name}</option>
                                        </c:forEach>
                                    </select>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td class="ui_text_rt" width="140">菜单</td>
                    <td class="ui_text_lt">
                        <table>
                            <tr>
                                <td>
                                    <select multiple="true" class="ui_multiselect01" id="all_menus">
                                        <c:forEach items="${menus}" var="item">
                                            <option value="${item.id}">${item.name}</option>
                                        </c:forEach>
                                    </select>
                                </td>
                                <td align="center">
                                    <input type="button" id="mselect" value="-->" class="left2right"/><br/>
                                    <input type="button" id="mselectAll" value="==>" class="left2right"/><br/>
                                    <input type="button" id="mdeselect" value="<--" class="left2right"/><br/>
                                    <input type="button" id="mdeselectAll" value="<==" class="left2right"/>
                                </td>
                                <td>
                                    <select multiple="true" class="ui_multiselect01" name="menuIds" id="self_menus">
                                        <c:forEach items="${role.menus}" var="item">
                                            <option value="${item.id}">${item.name}</option>
                                        </c:forEach>
                                    </select>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td class="ui_text_lt">
                        &nbsp;<input type="submit" value="确定保存" class="ui_input_btn01" />
                        &nbsp;<input id="cancelbutton" type="button" value="重置" class="ui_input_btn01"/>
                    </td>
                </tr>
            </table>
        </div>
    </div>
</form>
</body>
</html>